package de.haw.avl.fileReader;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class FileReader {

	private final File file;
	private ArrayList<Integer> result;

	public FileReader(File file) {
		this.file = file;
	}

	public FileReader() {
		this(new File("C:\\Users\\abi810\\Downloads\\zzahlen.dat"));
	}

	public ArrayList<Integer> read() {
		result = new ArrayList<>();

		try {
			RandomAccessFile rF = new RandomAccessFile(file, "rw");
			String line = rF.readLine();
			String[] resultList = line.split(" ");
			for (String string : resultList) {
				Integer zahl = Integer.parseInt(string.trim());
				if (!result.contains(zahl)) {
					result.add(zahl);
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return result;
	}

	/**
	 * 
	 * @return
	 */
	public Integer controlSum() {
		Integer i = 0;
		for (Integer integer : result) {
			i += integer;
		}
		return i;
	}

}
